package idiet.dao.hibernate;

import idiet.bean.FoodMaterial;
import idiet.bean.FoodMaterialCategory;
import idiet.dao.SqlStatementHelper;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository("foodMaterialDao")
public class FoodMaterialDao {
	@Autowired
	SessionFactory sessionFactory;
	@Autowired
	SqlStatementHelper sqlStatementHelper;
	@SuppressWarnings("unchecked")
	public List<FoodMaterial> getFoodMaterialsByMaterialName(String materialName){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(FoodMaterial.class)
				.add(Restrictions.like("name", materialName))
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<FoodMaterial> getFoodMaterials(){
		return this.sessionFactory
				.openSession()
				.createCriteria(FoodMaterial.class)
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<FoodMaterial> getFoodMaterialsBySecondaryCategory(FoodMaterialCategory category){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(FoodMaterial.class)
				.add(Restrictions.eq("foodMaterialCategory", category))
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<FoodMaterial> getFoodMaterialsBySecondaryCategory(FoodMaterialCategory category,String materialName){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(FoodMaterial.class)
				.add(Restrictions.eq("foodMaterialCategory", category))
				.add(Restrictions.like("name", materialName))
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<FoodMaterial> getFoodMaterialBySecondaryCategories(List<FoodMaterialCategory> categories){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(FoodMaterial.class)
				.add(Restrictions.in("foodMaterialCategory", categories))
				.list();
	}
	@SuppressWarnings("unchecked")
	public List<FoodMaterial> getFoodMaterialBySecondaryCategories(List<FoodMaterialCategory> categories,String materialName){
		return this.sessionFactory
				.getCurrentSession()
				.createCriteria(FoodMaterial.class)
				.add(Restrictions.in("foodMaterialCategory", categories))
				.add(Restrictions.like("name", materialName))
				.list();
	}
	public static void main(String[] args){
		FoodMaterialDao dao=new FoodMaterialDao();
		List<FoodMaterial> materials= dao.getFoodMaterials();
		System.out.println(materials.size());
	}
}
